/*
	Copyright (c) 2010, Mathias Myrland, Myrland Systemutvikling (mathias@myrsys.no)
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
	
	    * Redistributions of source code must retain the above copyright
	      notice, this list of conditions and the following disclaimer.
	      
	    * Redistributions in binary form must reproduce the above copyright
	      notice, this list of conditions and the following disclaimer in the
	      documentation and/or other materials provided with the distribution.
	      
	    * Neither the name of Myrland Systemutvikling nor the
	      names of its contributors may be used to endorse or promote products
	      derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	DISCLAIMED. IN NO EVENT SHALL Myrland Systemutvikling BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CGF_COMPONENT_FACTORY_HPP
#define CGF_COMPONENT_FACTORY_HPP

#include <cgf/object.hpp>

#include <boost/unordered_map.hpp>
#include <boost/function.hpp>

#include <cgf/component.hpp>
#include <cgf/managedcontainer.hpp>
#include <cgf/entity.hpp>
#include <cgf/event.hpp>
#include <cgf/attributefactory.hpp>

namespace CGF
{
	class Event;
	
	/**
	*	The component factory manages templates and creates components based on the templates	*/
	class ComponentFactory : public Object<ComponentFactory>
	{
		
		/**	The templates loaded into this factory	*/
		boost::unordered_map<std::string, ptr_ComponentTemplate> m_templates;
		
		/**	Component creation function map	*/
		boost::unordered_map<std::string, boost::function<Object<Component>::ptr (Object<ComponentTemplate>::ptr, Object<Entity>::ptr )> > m_componentCreators;
		
		/**	A container of all the components produced by this factory instance	*/
		boost::shared_ptr<ManagedContainer<Component> >	m_created;
		
		/**	A container for all the components that will be updated through the update() call (every component with the updatable class)	*/
		boost::shared_ptr<ManagedContainer<Component> > m_updatable;
		
		/**	The events currently in existence	*/
		boost::shared_ptr<ManagedContainer<Event> >	m_events;
		
		uint64_t m_logicTick;
		
		boost::shared_ptr<AttributeFactory<Component> > m_attributeFactory;
		
		boost::weak_ptr<EntityFactory> m_entityFactory;
		
		public:
			
			/**
			*	Checks if a template exists or not	*/
			bool hasTemplate ( std::string key ) { return (bool)m_templates.count(key); } 
			boost::weak_ptr<EntityFactory>& entityFactory () { return m_entityFactory; }
			
			boost::shared_ptr<AttributeFactory<Component> >& attributeFactory() { return m_attributeFactory; }
			
			/**
			*	Create a component based on a template name. Set the owner of the
			*	component to the entity that is passed to the function.
			*
			*	@param	name	The name of the template to use when creating the component
			*	@param	entity	The entity that will be the owner of the new component
			*	@return		A shared pointer to the newly created component	*/
			ptr_Component createComponent ( std::string, ptr_Entity );
		
			boost::unordered_map<std::string, boost::function<Object<Component>::ptr (Object<ComponentTemplate>::ptr, Object<Entity>::ptr )> > & componentCreators() { return m_componentCreators; }
			
			/**
			*	We expose the created list	*/
			boost::shared_ptr<ManagedContainer<Component> >& created() { return m_created; }
			
			
			/**
			*	Expose the event list as well	*/
			boost::shared_ptr<ManagedContainer<Event> >& events() { return m_events; }
			
			/**
			*	Register a base class creator function so that the class is accessible to the template creator.
			*
			*	@param	name	The name of this baseclass
			*	@param	func	The function returning the new object	*/
			void registerBaseclass ( std::string, boost::function<Object<Component>::ptr (Object<ComponentTemplate>::ptr, Object<Entity>::ptr )> );
			
			/**
			*	Update a logic tick in all the components monitored by the factory	*/
			void update ( );
			
			/**
			*	Read the template specifications from a xml file, and store them in the template array.
			*
			*	@param	fileName	The path of the xml file containing the template specifications	*/
			void loadTemplates ( std::string );
		
			ComponentFactory();
			~ComponentFactory();
	};
	
}//	namespace CGF

#endif
